package io.kiki.stack.http.feign.SpringMvcContract.encoding;

import io.kiki.stack.http.feign.RequestInterceptor;
import io.kiki.stack.http.feign.RequestTemplate;
import org.springframework.util.Assert;

/**
 * The base request interceptor.
 */
public abstract class BaseRequestInterceptor implements RequestInterceptor {

    /**
     * The encoding properties.
     */
    private final FeignClientEncodingProperties feignClientEncodingProperties;

    /**
     * Creates new instance of {@link BaseRequestInterceptor}.
     *
     * @param feignClientEncodingProperties the encoding properties
     */
    protected BaseRequestInterceptor(FeignClientEncodingProperties feignClientEncodingProperties) {
        Assert.notNull(feignClientEncodingProperties, "Properties can not be null");
        this.feignClientEncodingProperties = feignClientEncodingProperties;
    }

    /**
     * Adds the header if it wasn't yet specified.
     *
     * @param requestTemplate the request
     * @param name            the header name
     * @param values          the header values
     */
    protected void addHeader(RequestTemplate requestTemplate, String name, String... values) {

        if (!requestTemplate.headers().containsKey(name)) {
            requestTemplate.header(name, values);
        }
    }

    protected FeignClientEncodingProperties getFeignClientEncodingProperties() {
        return this.feignClientEncodingProperties;
    }

}
